import json
import requests
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# requests 的 verify 设置为False时，需要设置不安全请求提醒忽略

class RunMethod:
    def __init__(self):
        self.session = requests.session()

    def post_main(self, url, data, header=None):
        if header is None:
            res = self.session.post(url=url, json=data, verify=False)
        elif header["Content-Type"] == "application/x-www-form-urlencoded":
            res = self.session.post(url=url, data=data, headers=header, verify=False)
        else:
            res = self.session.post(url=url, json=data, headers=header, verify=False)
            # res = self.session.post(url=url, data=data, headers=header, verify=False)
        return res

    def get_main(self, url, data=None, header=None):
        if header is not None:
            res = requests.get(url=url, params=data, headers=header, verify=False)
        else:
            res = requests.get(url=url, params=data, verify=False)
        return res.json()

    def run_main(self, method, url, data=None, header=None):
        if method == 'Post':
            res = self.post_main(url, data, header)
        else:
            res = self.get_main(url, data, header)
        return res
        # return json.dumps(res, indent=2, sort_keys=True, ensure_ascii=False)


if __name__ == '__main__':
    url = 'http://httpbin.org/post'
    data = {
        'cart': '11'
    }
    run = RunMethod()
    run_test = run.run_main(method="Post", url=url, data=data)
    print(run_test)
